//
//  HomeController.m
//  SoundRecording
//
//  Created by zhengju on 2017/11/29.
//  Copyright © 2017年 郑俱. All rights reserved.
//

#import "HomeController.h"
#import "RecordingsController.h"
#import "ZJRecorder.h"
#import "Waver.h"


#define kRecordAudioFile @"myRecord.caf"

@interface HomeController ()
{
    BOOL isRecording;
}
@property (weak, nonatomic) IBOutlet UIButton *recorderBtn;
@property(strong,nonatomic) ZJRecorder * recorder;
@property(strong,nonatomic) Waver * waver;
@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"录音";
    
    self.view.backgroundColor = [UIColor redColor];
    self.recorder = [[ZJRecorder alloc]init];

    [self.rightBtn setTitle:@"列表" forState:UIControlStateNormal];
    @weakify(self)
    [self.rightBtn bk_addEventHandler:^(id sender) {
        
        @strongify(self);
        RecordingsController * vc = [[RecordingsController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    } forControlEvents:UIControlEventTouchUpInside];

}

- (IBAction)recorderStart:(UIButton *)sender {

    if(!isRecording){//开始录音
        //设置路径
        isRecording = YES;
        
        NSDate *  date=[NSDate date];
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"YYYY-MM-dd"];
        NSString *datefloder= [dateformatter stringFromDate:date];
    
        NSString *   dateaudioPath=[NSString stringWithFormat:@"%@/",datefloder];

        NSString * audioPath = @"zhengju";
        
        //指向文件目录
        NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        NSString *  audioRecoderSavePath=[NSString stringWithFormat:@"%@/%@%@", documentsDirectory,audioPath,dateaudioPath];

        
        [dateformatter setDateFormat:@"hhmmss"];
        NSString * filePath= [NSString stringWithFormat:@"%@.caf",[dateformatter stringFromDate:date]] ;
        
        
        [self.recorder recorderSetPath: [audioRecoderSavePath stringByAppendingPathComponent:filePath]];
        
        [self.recorder recorderStart];
        
        [self.recorderBtn setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
        
        
        [self addWaver];
       
        
    }else{//关闭录音
        isRecording = NO;
        [self.recorder recorderStop];
        [self.recorderBtn setImage:[UIImage imageNamed:@"录音"] forState:UIControlStateNormal];
        [self.waver removeFromSuperview];
        self.waver = nil;
    }

}
- (void)addWaver{

    self.waver = [[Waver alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)/2.0 - 50.0, CGRectGetWidth(self.view.bounds), 100.0)];
    
    __block AVAudioRecorder *weakRecorder = [ZJRecorder shareRecorder].recorder;
    
    self.waver.waverLevelCallback = ^(Waver * waver) {
        
        [weakRecorder updateMeters];
        
        CGFloat normalizedValue = pow (10, [weakRecorder averagePowerForChannel:0] / 40);
        
        waver.level = normalizedValue;
        
    };
    [self.view addSubview:self.waver];
    
}


- (IBAction)recorderPlay:(UIButton *)sender {
  // [self.recorder recorderPlay];
    
}

@end
